package com.example.rss_reder;


import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.util.EncodingUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.text.Layout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter{

	Context context;
	ArrayList<List> list;
	LayoutInflater inflater;


	MyAdapter(Context c, ArrayList<List> l){
		this.context = c;
		this.list = l;

		inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0).getName();
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		if(arg1 == null){
			arg1 = inflater.inflate(R.layout.list, arg2, false);
		}
		TextView tv1 = (TextView)arg1.findViewById(R.id.tv_tit);
		TextView tv2 = (TextView)arg1.findViewById(R.id.tv_date);
		ImageView iv = (ImageView)arg1.findViewById(R.id.iv);

		LinearLayout ll = (LinearLayout)arg1.findViewById(R.id.data);
		ll.setBackgroundColor(Color.WHITE);
		tv2.setVisibility(View.VISIBLE);
		iv.setVisibility(View.GONE);

		if(list.get(arg0).getLink()==null){
			tv1.setText(list.get(arg0).getName());

			if(list.get(arg0).getDate()==null){
				ll.setBackgroundColor(Color.GRAY);
				tv2.setVisibility(View.GONE);
			}
			else{
				tv2.setText(list.get(arg0).getDate());
			}
			iv.setVisibility(View.GONE);
		}else{
			tv1.setText(list.get(arg0).getName());

			String link =list.get(arg0).getDate();
			tv2.setText(link);


			try{
				String img = list.get(arg0).getImg();
				if(img!=null){
					InputStream is=new URL(img).openStream();
					Bitmap bit = BitmapFactory.decodeStream(is);
					Bitmap b = Bitmap.createScaledBitmap(bit, 75, 75, false);
					iv.setVisibility(View.VISIBLE);
					iv.setImageBitmap(b);
					is.close();
				}else{
					iv.setImageBitmap(null);
				}
			}catch (Exception e){
				Log.e("dd2","",e);
			}

			//		 if(link.length()>=50){
			//			 link = link.substring(0,50);
			//		 }
		}

		return arg1;
	}

}
